package com.lgkj.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.bean.ShopTrendsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/25 0025.
 */

public class ShopTrendsAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<ShopTrendsBean.DataBeanX.DataBean> data;
    private ShopAmbientAdapter.MyClickListener mListener;
    private boolean isDel;

    public void setDel(boolean del) {
        isDel = del;
    }

    public void setClick(ShopAmbientAdapter.MyClickListener listener) {
        this.mListener = listener;
    }

    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }

    public ShopTrendsAdapter(Context context) {
        this.context = context;
        isDel = true;
        this.data = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public ShopTrendsBean.DataBeanX.DataBean getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_shop_trends, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (isDel) {
            holder.ivDel.setVisibility(View.VISIBLE);
            holder.tvJubao.setVisibility(View.GONE);
        } else {
            holder.ivDel.setVisibility(View.GONE);
            holder.tvJubao.setVisibility(View.VISIBLE);
        }
        holder.ivDel.setOnClickListener(this);
        holder.ivDel.setTag(i);
        holder.tvJubao.setOnClickListener(this);
        holder.tvJubao.setTag(i);
        try {
            holder.tvType.setTextColor(Color.parseColor(data.get(i).getTag_color()));
        } catch (Exception e) {
            Log.e("Tag_color", "getView: " + e.getMessage());
        }
        holder.tvType.setText(data.get(i).getTitle());
        holder.tvTime.setText(data.get(i).getCreate_time());
        holder.tvDesc.setText(data.get(i).getContent());
        return view;
    }

    public void setData(List<ShopTrendsBean.DataBeanX.DataBean> data) {
        if (data != null) {
            this.data.addAll(data);
        }
    }

    public void dataClean() {
        this.data.clear();
    }


    static class ViewHolder {
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_jubao)
        TextView tvJubao;
        @BindView(R.id.iv_del)
        ImageView ivDel;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_desc)
        TextView tvDesc;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //响应按钮点击事件,调用子定义接口，并传入View
    @Override
    public void onClick(View v) {
        mListener.clickListener(v);
    }
}
